Skocz do zawartości
  • 👋 Witaj na MPCForum!

    Przeglądasz forum jako gość, co oznacza, że wiele świetnych funkcji jest jeszcze przed Tobą! 😎

    • Pełny dostęp do działów i ukrytych treści
    • Możliwość pisania i odpowiadania w tematach
    • System prywatnych wiadomości
    • Zbieranie reputacji i rozwijanie swojego profilu
    • Członkostwo w jednej z największych społeczności graczy

    👉 Dołączenie zajmie Ci mniej niż minutę – a zyskasz znacznie więcej!

    Zarejestruj się teraz

[Pytanie] Jak zrobić sojusze?


Rekomendowane odpowiedzi

Opublikowano

Tak jak w temacie

Wiecie, może jak je zrobić ? 

Jeśli ktoś może to niech opisze albo po prostu da kod xD

I jeśli możecie to jak zrobić TAGI na TAB'ie ? 

Dam like oczywiście za pomoc ;)

Opublikowano

@up chodzi mi o to aby zrobić komendę, np /sojusz i żeby można było zapraszać innych i oni nie mogą się bić, ale inne sojusze tak.

Opublikowano

Dobra... smerfa ma dobry dzień...

Znalazłam na dysku mój bardzo stary kod na sojusze :P

 

Nie jest może jakoś specjalnie dobry, ale działał!

 

Komenda:

 

 

package com.gmail.bukkitSmerf.cmd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.gmail.bukkitSmerf.minelive.Cfg;
import com.gmail.bukkitSmerf.minelive.Ml;
import com.gmail.bukkitSmerf.minelive.Util;

public class Sojusz implements CommandExecutor {

	private Ml plugin;

	public Sojusz(Ml plugin) {
		this.plugin = plugin;
	}

	private static Map<String, String> check = new HashMap<String, String>();
	private static Map<String, List<String>> block = new HashMap<String, List<String>>();

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String label,
			String[] args) {
		if (sender instanceof Player) {
			final Player player = (Player) sender;
			final String playerName = player.getName();
			if (args.length == 1 && args[0].equalsIgnoreCase("tak")) {
				if (check.containsKey(playerName)) {
					String[] data = check.get(playerName).split("-");
					Cfg.addAlliance(playerName, data[0],
							Long.parseLong(data[1]));
					Util.sendMsg(
							"&9Przyjeles propozycje sojuszu z "
									+ data[0]
									+ ". Na czas sojuszu nie mozecice sie atakowac, 5 minut przed koncem dostaniecie info.",
							player);
					Util.sendMsg(
							"&5"
									+ playerName
									+ " &aZgodzil sie na twoja propozycje sojuszu! 5 minut przed koncem dostaniecie info.",
							Bukkit.getPlayer(data[0]));
					return true;
				}
			} else if (args.length > 1 && args[0].equalsIgnoreCase("-block")) {
				String r = args[1];
				Player p = Bukkit.getPlayer(r);
				if (p != null)
					r = p.getName().toLowerCase();
				List<String> blocked = new ArrayList<String>();
				if (block.containsKey(playerName))
					blocked = block.get(playerName);
				if (blocked.contains(r))
					blocked.remove(r);
				else
					blocked.add(r);
				block.put(playerName, blocked);
				Util.sendMsg(
						"&aZablokowales przyjmowanie zaproszej sojuszu od &8"
								+ r
								+ "\n&9Pamietaj ze blokada minie po resecie/reloadzie serwera  jesli chcesz go odblokowac wpisz komende jeszcze raz.",
						player);
				return true;
			} else if (args.length > 1) {
				Player target = Bukkit.getPlayer(args[0]);
				if (target != null) {
					final String targetName = target.getName();
					if (block.containsKey(targetName)
							&& block.get(playerName).contains(
									playerName.toLowerCase())) {
						Util.sendMsg(
								"&4Ten gracz cie zablokowal, nie mozesz wysylac do niego propozycji sojuszu.",
								player);
						return true;
					}
					long time = 0l;
					try {
						time = Util.parseDateDiff(args[1], true);
					} catch (Exception e) {
						Util.sendMsg(
								"&4Zle podales czas. Podaj czas w takiej formie: &94h20m &c- 4 godziny i 20 minut",
								player);
						return true;
					}
					check.put(targetName, playerName + "-" + time);
					Util.sendMsg(
							"&aZaproszenie wyslane, poczekaj na odpowiedz &9"
									+ targetName + ".", player);
					Util.sendMsg(
							"&bDostales/as zaproszenie do sojuszu od: &9"
									+ playerName
									+ " &bna czas: &6"
									+ Util.getTime(time)
									+ "&bBy przyjac wpisz &d/Sojusz tak\n&9Jesli chcesz zablokowac zaproszenia od tego gracza wpisz /Sojusz -block "
									+ playerName, target);
					Bukkit.getScheduler().runTaskLater(plugin, new Runnable() {
						@Override
						public void run() {
							check.remove(player);
							if (!Cfg.isAlliance(playerName, targetName))
								Util.sendMsg(
										"&cTwoja propozycja sojuszu zostala odrzucona.",
										player);
						}
					}, 200);
				} else {
					Util.sendMsg("&4Nie ma takiego gracza na serwerze.", player);
					return true;
				}
			} else {
				Util.sendMsg(
						"&9Komenda ta pozwala na utworzenie sojuszu miedzy graczami. Dzieki temu nie moga sie atakac. (miecze, luki, potki w tym trucizny)\n&9Wpisz &b/Sojusz <Nick> <czas>&9 by wyslac zapytanie o sojusz, wpisz &b/Sojusz tak&9 by przyjac zapytanie. np: \n&b/Sojusz Tilli 2g30m &9Pytasz Tille o sojusz na 2 godziny i 30 min. \n&4Wybieraj czas ostroznie, sojuszu nie da sie przerwac. (moze potem sie ulituje i to dodam).",
						player);
			}
		}
		return true;
	}

}

 

 

Event - EntityDamageByEntityEvent

 

 

package com.gmail.bukkitSmerf.listener;

import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import com.gmail.bukkitSmerf.minelive.Cfg;
import com.gmail.bukkitSmerf.minelive.Util;

public class EntityDmg implements Listener {

	@EventHandler
	public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent ev) {
		Entity damager = ev.getDamager();
		Entity target = ev.getEntity();
		if (target instanceof Player) {
			String targetName = ((Player) target).getName();
			String damagerName = null;
			if (damager instanceof Player)
				damagerName = ((Player) damager).getName();
			else if (damager instanceof Arrow) {
				Arrow arrow = (Arrow) damager;
				LivingEntity shooter = arrow.getShooter();
				if (shooter instanceof Player)
					damagerName = ((Player) shooter).getName();
				else
					return;
			} else
				return;
			if (Cfg.isAlliance(damagerName, targetName)) {
				ev.setCancelled(true);
				Util.sendMsg(
						"&9Masz sojusz z tym graczem, nie mozesz go atakowac.",
						((Player) damager));
				return;
			}
		}
	}
}

 

 

W kliku miejscach jak widać są jakieś metody z "cfg" więc mały  opis:

 

 

więc w miejscu gdzie przetrzymuje konfiguracje mam mapkę:

	public static Map<String, Map<String, Long>> alliances = new HashMap<String, Map<String, Long>>();

Zapisuje ona sojusze dla danego gracza, wraz z czasem ich końca.

I mam tam takie metody jak:


	public static void addAlliance(String player, String target, long time) {
		if (alliances.containsKey(player))
			alliances.get(player).put(target, time);
		else {
			Map<String, Long> data = new HashMap<String, Long>();
			data.put(target, time);
			alliances.put(player, data);
		}
		if (alliances.containsKey(target))
			alliances.get(target).put(player, time);
		else {
			Map<String, Long> data = new HashMap<String, Long>();
			data.put(player, time);
			alliances.put(target, data);
		}

	}

	public static void removeAlliance(String player, String target, long time) {
		if (alliances.containsKey(player))
			if (alliances.get(player).containsKey(target)) {
				alliances.get(player).remove(target);
				if (alliances.get(player).isEmpty())
					alliances.remove(player);
			}
		if (alliances.containsKey(target))
			if (alliances.get(target).containsKey(player)) {
				alliances.get(target).remove(player);
				if (alliances.get(target).isEmpty())
					alliances.remove(target);
			}
	}

	public static boolean isAlliance(String player, String target) {
		if (alliances.containsKey(player) && alliances.containsKey(target))
			return (alliances.get(player).containsKey(target) && alliances.get(
					target).containsKey(player));
		else
			return false;

	}
	
	private static File file2;
	private static FileConfiguration s;

	public static void saveAlliance() {
		Map<String, Long> data = new HashMap<String, Long>();
		s.set("a", null);
		for (String player : alliances.keySet()) {
			data = alliances.get(player);
			for (String player2 : data.keySet()) {
				s.set("a." + player + "." + player2, data.get(player2));
			}
		}
		try {
			s.save(file2);
		} catch (IOException e) {
		}
	} 

 

 

Kolejnym eventem jest PotionSplashEvent, który uniemożliwia nadanie sojusznikowi złego efektu potka, (te dobre działają)

 

 

package com.gmail.bukkitSmerf.listener;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.PotionSplashEvent;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import com.gmail.bukkitSmerf.minelive.Cfg;
import com.gmail.bukkitSmerf.minelive.Util;

public class PotionSplash implements Listener {
	@EventHandler
	public void onPotionSplashEvent(PotionSplashEvent ev) {
		boolean isNegative = false;
		LivingEntity potionShooter = ev.getEntity().getShooter();
		for (PotionEffect pe : ev.getPotion().getEffects())
			if (pe.getType().equals(PotionEffectType.ABSORPTION)
					|| pe.getType().equals(PotionEffectType.BLINDNESS)
					|| pe.getType().equals(PotionEffectType.CONFUSION)
					|| pe.getType().equals(PotionEffectType.HARM)
					|| pe.getType().equals(PotionEffectType.HUNGER)
					|| pe.getType().equals(PotionEffectType.POISON)
					|| pe.getType().equals(PotionEffectType.SLOW_DIGGING)
					|| pe.getType().equals(PotionEffectType.WEAKNESS)
					|| pe.getType().equals(PotionEffectType.SLOW)
					|| pe.getType().equals(PotionEffectType.WITHER))
				isNegative = true;
			else
				return;
		if (isNegative)
			for (LivingEntity entity : ev.getAffectedEntities()) {
				if (entity instanceof Player && potionShooter instanceof Player)
					if (Cfg.isAlliance(((Player) entity).getName(),
							((Player) potionShooter).getName())) {
						ev.setIntensity(entity, 0);
						ev.setIntensity(potionShooter, 0);
						Util.sendMsg(
								"&9Masz sojusz z tym graczem, nie mozesz go atakowac.",
								((Player) potionShooter));
						return;
					}
			}
	}
}

 

 

 

I to chyba wszystko, pewności nie mam, bo to bardzo stary kod :D a nie chciało mi się go nawet importować do Eclipse, więc kopiowałam z notatnika.

 

 

EDIT: wiem że kod dość dużo, i nie jest też jakoś specjalnie dobry, ale jednak ma też dużo możliwości :> 

(PS: o ile pamiętam da się zakładać sojusze sam ze sobą)

 

 

EDIT2:

Zapomniałam o tym: Util.parseDateDiff

Kod z tej metody pochodzi w 100% od essentials jak coś :P

 

 


	public static long parseDateDiff(String czas, boolean future)
			throws Exception {
		Pattern timePattern = Pattern.compile("(?[0-9]+)\\s*y[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*mo[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*w[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*d[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*h[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*m[a-z]*[,\\s]*)?"
				+ "(?[0-9]+)\\s*(?[a-z]*)?)?", Pattern.CASE_INSENSITIVE);
		Matcher m = timePattern.matcher(czas);
		int years = 0;
		int months = 0;
		int weeks = 0;
		int days = 0;
		int hours = 0;
		int minutes = 0;
		int seconds = 0;
		boolean found = false;
		while (m.find()) {
			if (m.group() == null || m.group().isEmpty()) {
				continue;
			}
			for (int i = 0; i < m.groupCount(); i++) {
				if (m.group(i) != null && !m.group(i).isEmpty()) {
					found = true;
					break;
				}
			}
			if (found) {
				if (m.group(1) != null && !m.group(1).isEmpty()) {
					years = Integer.parseInt(m.group(1));
				}
				if (m.group(2) != null && !m.group(2).isEmpty()) {
					months = Integer.parseInt(m.group(2));
				}
				if (m.group(3) != null && !m.group(3).isEmpty()) {
					weeks = Integer.parseInt(m.group(3));
				}
				if (m.group(4) != null && !m.group(4).isEmpty()) {
					days = Integer.parseInt(m.group(4));
				}
				if (m.group(5) != null && !m.group(5).isEmpty()) {
					hours = Integer.parseInt(m.group(5));
				}
				if (m.group(6) != null && !m.group(6).isEmpty()) {
					minutes = Integer.parseInt(m.group(6));
				}
				if (m.group(7) != null && !m.group(7).isEmpty()) {
					seconds = Integer.parseInt(m.group(7));
				}
				break;
			}
		}
		Calendar c = new GregorianCalendar();
		if (years > 0) {
			c.add(Calendar.YEAR, years * (future ? 1 : -1));
		}
		if (months > 0) {
			c.add(Calendar.MONTH, months * (future ? 1 : -1));
		}
		if (weeks > 0) {
			c.add(Calendar.WEEK_OF_YEAR, weeks * (future ? 1 : -1));
		}
		if (days > 0) {
			c.add(Calendar.DAY_OF_MONTH, days * (future ? 1 : -1));
		}
		if (hours > 0) {
			c.add(Calendar.HOUR_OF_DAY, hours * (future ? 1 : -1));
		}
		if (minutes > 0) {
			c.add(Calendar.MINUTE, minutes * (future ? 1 : -1));
		}
		if (seconds > 0) {
			c.add(Calendar.SECOND, seconds * (future ? 1 : -1));
		}
		return c.getTimeInMillis();
	} 

 

 

 

 

EDIT3:

Mam jeszcze takie coś: (od wyświetlania wiadomości że sojusz sie kończy, i od auto-zapisu)

 

 


		Bukkit.getScheduler().runTaskTimer(this, new Runnable() {

			@Override
			public void run() {
				for (String playerName : Cfg.alliances.keySet()) {
					Map<String, Long> playerAlliances = Cfg.alliances
							.get(playerName);
					for (String player2Name : playerAlliances.keySet()) {
						long toEndTime = playerAlliances.get(player2Name)
								- new Date().getTime();
						if (toEndTime <= 60000 && toEndTime > 35000) {
							Player player = Bukkit.getPlayer(playerName);
							if (player != null)
								Util.sendMsg(
										"&4Twoj sojusz z &c"
												+ player2Name
												+ " &4 skonczy sie za mniej niz 60 sekund.\n&9Mozesz przedluzyc czas ponownie zakladajac sojusz.",
										player);
						} else if (toEndTime <= 300000 && toEndTime > 230000) {
							Player player = Bukkit.getPlayer(playerName);
							if (player != null)
								Util.sendMsg(
										"&4Twoj sojusz z &c"
												+ player2Name
												+ " &4 skonczy sie za okolo 5 minut.\n&9Mozesz przedluzyc czas ponownie zakladajac sojusz.",
										player);
						} else if (toEndTime <= 0) {
							playerAlliances.remove(player2Name);
							Player player = Bukkit.getPlayer(playerName);
							if (player != null)
								Util.sendMsg("&4Twoj sojusz z &c" + player2Name
										+ " &4 sie zakonczyl.", player);
						}
					}
				}
			}

		}, 750l, 750l);
		Bukkit.getScheduler().runTaskTimer(this, new Runnable() {

			@Override
			public void run() {
				Ml.log(Level.ALL, "&fZapisywanie sojuszy...");
				Cfg.saveAlliance();
				Ml.log(Level.ALL, "&fZapisywanie sojuszy zakonczone.");
			}

		}, 36000l, 36000l);

 

 

To już jest koniec smerfa:


http://www.mpcforum.pl/topic/1323530-info-znikam/


GG: 48522543


PS: Na innych forach i stronach znajdziesz mnie pod nickiem: 


BukkitSmerf

Zarchiwizowany

Ten temat przebywa obecnie w archiwum. Dodawanie nowych odpowiedzi zostało zablokowane.

×
×
  • Dodaj nową pozycję...